<?php
/**
 * 消息推送管理
 * 
 * @author  Lee<a605333742@gmail.com>
 * @time    2016-08-14
 */
namespace Admin\Controller;

class MessageController extends CommonController{
    /*
     * 构造函数
     * 
     * @return
     */
    public function _initialize() {
        parent::_initialize();
        $this->db   =new \Model\MessageModel();
    }
    
    /*
     * 消息列表
     * 
     * @return #
     */
    public function index(){
        if(I("request.content")){
            $map['content'] =I("request.content");
        }
        $map            =$this->_search("Message");
        $map['status']  =1;
        $messageList    =$this->_list($this->db,$map);
        $this->assign("mList",$messageList);
        $this->display();
    }
    
    /*
     * 显示新增消息
     * 
     * @return  #
     */
    public function show(){
        $this->display();
    }
    
    /*
     * 新增消息推送
     * 
     * @return #
     */
    public function edit(){
        if(I("request.content")){
            $map['content'] =I("request.content");
        }
        if(I("request.type")==1){
            $map['receiver']='all';
            $map['type']    =2;
        }else if(I("request.type")==2){
            $map['receiver']='android';
            $map['type']    =2;
        }else if(I("request.type")==3){
            $map['receiver']='ios';
            $map['type']    =2;
        }else if(I("request.type")==4){
            $map['receiver']='all';
            $map['type']    =1;
            if(I("request.new_id")){
                $map['new_id']  =I("request.new_id");
            }
        }
        $map['title']   ="涂友";
        
        if(I("request.jurl")){
            $data['url']=I("request.jurl");
        }
        $data['id']     =$map['new_id'];
        $data['type']   =$map['type'];
        $map['m_type']  ='http';
        $map['m_time']  =60*60*24;          //离线保留时间$receive,$content,$m_type,$m_txt,$m_time
        
        $pushObj=new \Component\Jpush();
        $result =$pushObj->push($map['receiver'],$map['content'],$map['m_type'],str_replace('\\','',json_encode($data)),$map['m_time'],$map['title']);

        if($result){
            //show_bug($result);exit;
            $res_arr = json_decode($result, true);
            if(isset($res_arr['error'])){                       //如果返回了error则证明失败
                //echo $res_arr['error']['message'];              //错误信息
                //echo $res_arr['error']['code'];                 //错误码
                //return false;
                $this->error("消息推送失败");
            }else{
                //处理成功的推送......
                //echo '推送成功.....';
                //return true;
                if($this->db->edit($map)){
                    $this->success("消息推送成功");
                }else{
                    $this->error("消息推送失败");
                }
            }
        }else{      //接口调用失败或无响应
            //echo '接口调用失败或无响应';
            //return false;
            $this->error("消息推送失败");
        }
    }
    
    /*
     * 删除推送信息
     * 
     * @return #
     */
    public function del(){
        if(!I("request.id")){
            $this->error("参数错误");
        }
        $map['id']      =I("request.id");
        $map['status']  =0;
        if($this->db->del($map)){
            $this->success("删除成功");
        }else{
            $this->error("删除失败");
        }
    }
}